package com.toocms.jinjuhang.ui.mine.order

import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.RelativeSizeSpan
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.data.order_info.OrderCommodityItemData
import com.toocms.jinjuhang.dialog.order_cancel.OrderCancelDialog
import com.toocms.jinjuhang.ui.mine.order.evaluate.OrderEvaluateFgt
import com.toocms.tab.base.BaseActivity
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.base.ItemViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.Messenger

class OrderServiceItemModel(
    viewModel: BaseViewModel<*>,
    val item: (ObservableField<OrderCommodityItemData>) = ObservableField(),
    val status: (ObservableField<OrderStatus>) = ObservableField(OrderStatus.AWAIT_PAYMENT),
    val isShowRefund: ObservableBoolean = ObservableBoolean(false)
) : ItemViewModel<BaseViewModel<*>>(viewModel) {

    val isVisibleFunction0 by lazy {
        val content = when (status.get()) {
            OrderStatus.AWAIT_EVALUATE -> {
                //判断是否评价
                if (Constants.Extra.ORDER_COMMODITY_EVALUATE_STATUS_NO == item.get()?.is_comm) true else false
            }
            else -> false
        }
        ObservableBoolean(content)
    }
    val function0Hint by lazy {
        val content = when (status.get()) {
            OrderStatus.AWAIT_EVALUATE -> {
                //判断是否评价
                if (Constants.Extra.ORDER_COMMODITY_EVALUATE_STATUS_NO == item.get()?.is_comm) StringUtils.getString(
                    R.string.str_evaluate
                )
                else ""
            }
            else -> ""
        }
        ObservableField(content)
    }
    val function0ClickBindingCommand = BindingCommand<BindingAction>(BindingAction {

        when (status.get()) {
            OrderStatus.AWAIT_EVALUATE -> {
                //判断是否评价
                if (Constants.Extra.ORDER_COMMODITY_EVALUATE_STATUS_NO == item.get()?.is_comm) {
                    //TODO 评价
                    item.get()?.order_id ?: return@BindingAction
                    item.get()?.order_goods_id ?: return@BindingAction
                    val bundle = Bundle()
                    bundle.putParcelable(Constants.BundleKey.KEY_ORDER_COMMODITY, item.get())
                    viewModel.startFragment(OrderEvaluateFgt::class.java, bundle)
                }
            }
            else -> {

            }
        }
    })

    val isVisibleFunction1 by lazy {
        val content = when (status.get()) {
            OrderStatus.AWAIT_RECEIVING -> isShowRefund.get() //TODO 退货/退款
            OrderStatus.AWAIT_EVALUATE -> isShowRefund.get() //TODO 退货/退款
            else -> false
        }
        ObservableBoolean(content)
    }

    val function1Hint by lazy {
        val content = when (status.get()) {
            OrderStatus.AWAIT_RECEIVING -> StringUtils.getString(R.string.str_sales_return_or_refund) //TODO 退货/退款
            OrderStatus.AWAIT_EVALUATE -> StringUtils.getString(R.string.str_sales_return_or_refund) //TODO 退货/退款
            else -> ""
        }
        ObservableField(content)
    }
    val function1ClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        val orderId = item.get()?.order_id ?: return@BindingAction
        val orderGoodsId = item.get()?.order_goods_id ?: return@BindingAction
        when (status.get()) {
            OrderStatus.AWAIT_RECEIVING, OrderStatus.AWAIT_EVALUATE -> {
                //TODO 退货/退款
                OrderCancelDialog().apply {
                    this.orderId = orderId
                    this.orderGoodsId = orderGoodsId
                    this.type = Constants.Extra.REASON_TYPE_APPLY_SALES
                    onSucceedListener = {
                        Messenger.getDefault()
                            .sendNoMsg(Constants.MessengerToken.TOKEN_REFRESH_ORDER)
                    }
                    show(
                        (ActivityUtils.getTopActivity() as BaseActivity).supportFragmentManager,
                        null
                    )
                }
            }
            else -> false
        }
    })


    fun price(commodityType: String?, sum: String?) = when (commodityType) {
        Constants.Extra.COMMODITY_TYPE_INTEGRAL -> {
            val sum = if (sum.isNullOrEmpty()) "00" else sum
            SpannableStringBuilder("$sum${StringUtils.getString(R.string.str_integral)}").apply {
                setSpan(
                    RelativeSizeSpan(0.8f), sum.length, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
            }
        }
        else -> {
            val sum = if (sum.isNullOrEmpty()) "0.00" else sum
            "${StringUtils.getString(R.string.str_money_symbol)}$sum"
        }
    }

}